/*
 * Copyright 2009 Andreas Borglin andreas.borglin@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package borge.mobile.app.jakoon;

import borge.mobile.app.jakoon.Main.ESwtExecutor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Form presented at startup of application
 * @author Andreas Borglin
 */
public class StartupForm {

    private Composite parent;
    private Composite top;
    private Composite saveComp;
    private Composite commandComp;
    private ESwtExecutor executor;

    private Text dirText;

    private static final int NUM_COLUMNS = 1;
    private static final String APP_TITLE = "Jakoon";
    private static final String APP_TEXT = "Jakoon is an image manipulation app written for "
                + "Sony Ericsson\nS60 640x360 resolution touchscreen devices.\n" +
                "With Jakoon you can manipulate images " +
                "in fullscreen landscape.\n" +
                "Go to http://borglin.net/code for more info and source code.";
    private static final String DEFAULT_PATH = "E:/Images/Camera/";

    protected StartupForm(ESwtExecutor exec, Composite parent) {

        this.parent = parent;
        executor = exec;

        top = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        top.setLayout(layout);

        saveComp = new Composite(top, SWT.NONE);
        layout = new GridLayout();
        layout.marginLeft = 20;
        layout.numColumns = NUM_COLUMNS;
        saveComp.setLayout(layout);

        commandComp = new Composite(top, SWT.NONE);
        layout = new GridLayout();
        layout.numColumns = 2;
        layout.marginLeft = 465;
        layout.marginTop = 0;
        commandComp.setLayout(layout);

        // Title, centered and spawns over all columns
        Label title = new Label(saveComp, SWT.NONE);
        title.setText(APP_TITLE);
        GridData data = new GridData();
        data.horizontalAlignment = GridData.CENTER;
        data.horizontalSpan = NUM_COLUMNS;
        data.heightHint = 40;
        title.setLayoutData(data);

        Label text = new Label(saveComp, SWT.WRAP);
        text.setText(APP_TEXT);

        initDirectoryArea();
        initCommandsArea();

        saveComp.layout();
        commandComp.layout();
        top.layout();

    }

    protected void makeVisible(){
        top.setVisible(true);
    }

    private void initDirectoryArea() {

        GridData data;

        // Directory stuff
        Label dirLabel = new Label(saveComp, SWT.NONE);
        dirLabel.setText("Root directory");
        data = new GridData();
        data.horizontalAlignment = GridData.BEGINNING;
        data.horizontalSpan = NUM_COLUMNS;
        dirLabel.setLayoutData(data);

        dirText = new Text(saveComp, SWT.SINGLE);
        String defPath = RMSHandler.getDefaultPath();
        if(defPath == null)
            defPath = DEFAULT_PATH;
        dirText.setText(defPath);
        data = new GridData();
        data.widthHint = 300;
        dirText.setLayoutData(data);

        Button dirBrowse = new Button(saveComp, SWT.PUSH);
        dirBrowse.setText("Browse");
        dirBrowse.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                DirectoryDialog dd = new DirectoryDialog(executor.getShell(), SWT.OPEN);
                dd.setMessage("Choose default path");
                String dir = dd.open();
                if(dir != null)
                    dirText.setText(dir);
            }
        });

        data = new GridData();
        data.horizontalAlignment = GridData.BEGINNING;
        data.horizontalSpan = NUM_COLUMNS-1;
        dirBrowse.setLayoutData(data);
    }


    private void initCommandsArea() {

        GridData data;

        Button save = new Button(commandComp, SWT.PUSH);
        save.setText("Proceed");
        save.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                executor.setDefaultPath(dirText.getText());
                top.setVisible(false);
                executor.loadImageForm(false);
            }
        });

        data = new GridData();
        save.setLayoutData(data);

        Button back = new Button(commandComp, SWT.PUSH);
        back.setText("Exit");
        back.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent event) {}
            public void widgetSelected(SelectionEvent event) {
                executor.exit();
            }
        });
        
        data = new GridData();
        back.setLayoutData(data);
    }

}
